package com.jokerpan.project.controller;

import com.jokerpan.project.annotation.AccessLimit;
import com.jokerpan.project.annotation.LogAction;
import com.jokerpan.project.commons.ActionResult;
import com.jokerpan.project.commons.ListResult;
import com.jokerpan.project.commons.ResultBuilder;
import com.jokerpan.project.entity.BackFiles;
import com.jokerpan.project.service.BackFilesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("backFiles")
@Api("附件操作Api")
@PreAuthorize("hasPermission('','admin:list')")
public class BackFilesController {

    @Resource
    BackFilesService backFilesService;

    @GetMapping("/queryList")
    @AccessLimit(seconds = 6, maxCount = 5)
    @ApiOperation("查询所有附件")
    public ListResult<Object> queryList(BackFiles backFiles) {
        return ResultBuilder.buildListSuccess(this.backFilesService.queryList(backFiles));
    }

    @GetMapping("/delete")
    @AccessLimit(seconds = 6, maxCount = 5)
    @ApiOperation("根据附件地址删除")
    @LogAction("附件管理,删除附件数据,删除附件,删除")
    @PreAuthorize("hasPermission('','admin:delete')")
    public ActionResult deleteByUrl(@RequestParam("url") String url) {
        if (backFilesService.deleteFile(url)){
            return ResultBuilder.buildActionSuccess();
        }
        return ResultBuilder.buildActionFailed();
    }
}
